package pro.ticktock.tools.mbconv;

import org.apache.poi.hwpf.converter.WordToTextConverter;

import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardOpenOption;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * Created by lambert on 2017/5/20.
 */
public class MusciBibleConverter{

  static int unmatchCount = 0;
  static int matchCount = 0;
  static int paraCount = 0;
  static int workCount = 0;
  static void finishPara() {
    paraCount++;
  }

  static void allFinished() {
    System.out.println("Paragraph count: " + paraCount);
    System.out.println("Matched/Unmatched brackets: " + matchCount + "/" + unmatchCount);
    System.out.println("Art work count: " + workCount);
  }


  /*
  boolean testParaStart(String line) {
    if (line == null || line.trim().length() == 0) {
      return false;
    }
    return leadingSpace.matcher(line).find();
  }
  */


  static String getRootDir() {
    return "D:\\workspace\\tmp-output\\mbible\\";
  }

  static String archiveDir = "D:\\workspace\\MusicBible-PDFs\\docs\\";

  static String[] fileList1 = new String[] {
          "a001-402.doc",
          //"a349-402.doc",
          "a403-440.doc",
          "a521-606.doc",
          //"a631-664.doc",
          "a739-760.doc",
          "b001-030.doc",
          "b051-070.doc",
          "b111-275.doc",
          "b341-365.doc",
          "b490-564.doc",
          "b621-722.doc",
          "b746-830.doc"
  };

  static String[] fileList = new String[] {
          "a001-402.doc"
  };

  private static final int LAST_PASS = 7;

  private static String finalFileName = "index";
  private static boolean sumTotalPagesOnly = false;

  public static void main(String[] args) throws Exception {

    int totalPages = Arrays.stream(fileList)
            .map(a -> a.substring(1, a.length() - 4).split("-"))
            .mapToInt(arr -> Integer.parseInt(arr[1]) - Integer.parseInt(arr[0]) + 1)
            .sum();
    System.out.println("Total pages: " + totalPages);

    if (sumTotalPagesOnly) return;

    String[] txtFileList = Arrays.stream(fileList)
            .map(file -> file.substring(0, file.indexOf('.')) + ".txt")
            .collect(Collectors.toList())
            .toArray(new String[0]);
    List<String> finalProcessedFiles = new ArrayList<>();

    String mergedFileName = "merged.txt";
    Path output = Paths.get(getRootDir() + mergedFileName);
    Files.deleteIfExists(output);
    Charset charset = StandardCharsets.UTF_8;

    for (int i = 0; i < fileList.length; i++) {
      WordToTextConverter.main(
              new String[] {
                      archiveDir + fileList[i],
                      getRootDir() + txtFileList[i]});
      Pass1 p1 = new Pass1(txtFileList[i]);
      p1.process();

      Pass2 p2 = new Pass2(txtFileList[i], p1.getResultFileName());
      p2.process();

      Path p = Paths.get(getRootDir() + p2.getResultFileName());
      List<String> lines = Files.readAllLines(p, charset);
      Files.write(output, lines, charset, StandardOpenOption.CREATE,
              StandardOpenOption.APPEND);
    }


    String inputFile = mergedFileName;
    for (int j = 3; j <= LAST_PASS; j++) {
      Pass p = (Pass)Pass.class.forName("pro.ticktock.tools.mbconv.Pass" + j)
              .getDeclaredConstructor(String.class, String.class)
              .newInstance(j < LAST_PASS ? mergedFileName : finalFileName, inputFile);
      p.process();
      inputFile = ((AbstractPass)p).getResultFileName();
    }
  }
}
